/**
 * @author: 王琛
 * @description: 通讯UE4
 * @Date: 2021-11-17 11:40:11
 * @param {*}
 * @return {*}
 */
export function toUe4({FuncName, Args}) {
    console.log('%c180329', 'color:#fff;background:#ee6f57;padding:3px;border-radius:2px', "UE4的参数", {
        FuncName,
        Args
    });
    ue4.zhiu_SendMsgToUe({FuncName, Args, time: +new Date()});
}

/**
 * @description: 如果只是单纯的使用webSoket连接ue调用此方法
 * @author: 王琛
 * @Date: 2022-12-28 15:20:48
 * @param {*}
 * @return {*}
 */

export class initWebSocket {
    _ws = null;
    _ws_state = 0;
    //
    _cache_notSent = [];
    
    toUe({FuncName, Args}) {
        const initUeData = {
            FuncName,
            Args,
            timeStamp: +new Date()
        }
        
        if (this._ws.readyState == 1 && this._ws?.send) {
            console.log('%c日志-112227', 'color:#fff;fontSize:15px;background:#993366;padding:5px 8px;border-radius:12px', "toUe", initUeData);
            
            this._ws.send(JSON.stringify(initUeData))
        } else {
            this._cache_notSent.push(initUeData);
            console.error('%c日志-112101', 'color:#fff;fontSize:15px;background:#993366;padding:5px 8px;border-radius:12px', "还没初始化完", initUeData);
        }
    }
    
    constructor({ip, message}) {
        // 创建连接
        this._ws = new WebSocket(ip);
        // 传输信息
        this._ws.onmessage = (data) => {
            // 获取解析的数据
            data.data.text().then(data => {
                console.log('%c日志-134902', 'color:#fff;fontSize:15px;background:#993366;padding:2px 5px;border-radius:6px', data);
                const data_Config = JSON.parse(data);
                console.log('%c日志-134833', 'color:#fff;fontSize:15px;background:#993366;padding:2px 5px;border-radius:6px', data_Config);
                if (!data_Config.Args.hasOwnProperty('Return')) message(data_Config)
            })
            
        }
        // 我连接成功了
        this._ws.onopen = () => {
            this._ws_state = 2;
            console.log('%c王琛-142842', 'color:#fff;background:#ee6f57;padding:3px;border-radius:2px', "ws连接成功");
            for (let i = 0, len = this._cache_notSent.length; i < len; i++) {
                const _cache_notSent_item = this._cache_notSent[i];
                this.toUe(_cache_notSent_item)
            }
            this._cache_notSent = [];
        }
        // 报错
        this._ws.onerror = (err) => {
            this._ws_state = 1;
            console.log('%c王琛-142842', 'color:#fff;background:#ee6f57;padding:3px;border-radius:2px', err, "ws连接报错");
        };
        // 关闭了
        this._ws.onclose = () => {
            this._ws_state = 1;
            console.log('%c王琛-142842', 'color:#fff;background:#ee6f57;padding:3px;border-radius:2px', "ws连接关闭");
        };
        
        
        return this;
    };
}

window['toUe4'] = toUe4;

/**
 * @author: 王琛
 * @description: 是否显示楼顶
 * @Date: 2021-11-17 11:42:36
 * @param {*}
 * @return {*}
 */
export function isShowRoofTop(data) {
    const parameter = {
        FuncName: "WebApi_VisibleBuild",
        Args: {
            visible: data
        }
    }
    toUe4(parameter);
}